package com.viphuli.app.tool.handler;

import java.util.Date;
import java.util.List;


import com.loopj.android.http.RequestParams;
import com.offroader.utils.DateUtils;
import com.offroader.utils.StringUtils;
import com.viphuli.app.tool.bean.page.ArrangeRecordDetailPage;
import com.viphuli.app.tool.bean.part.ArrangeRecordDetail;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.Constants;
import com.viphuli.app.tool.common.Constants.ArrangeRecordStatus;
import com.viphuli.app.tool.common.Constants.ArrangeRecordType;
import com.viphuli.app.tool.fragment.ArrangeRecordDetailArrangeFragment;
import com.viphuli.app.tool.http.ApiRequest;
import com.viphuli.app.tool.utils.OtherUtils;

public class ArrangeRecordDetailArrangeResponseHandler extends MyBaseHttpResponseHandler<ArrangeRecordDetailArrangeFragment, ArrangeRecordDetailPage> {

	@Override
	public void deal() {

		ArrangeRecordDetail detail = page.getArrangeRecordDetail();

		//排班类型
		ArrangeRecordType type = ArrangeRecordType.getType(detail.getType());
		caller.getTypeText().setText(type.getName());

		//时间
		caller.getTimeText().setText(DateUtils.format(detail.time()));

		//状态
		ArrangeRecordStatus status = ArrangeRecordStatus.getStatus(detail.getStatus());
		caller.getStatusText().setText(status.getName());
		
		List<Date> dateOfWeekList = DateUtils.dateToWeek(detail.getYear(), detail.getWhich());
		Date firstDayOfWeek = dateOfWeekList.iterator().next();

		//周时间
		String day = DateUtils.formatDay(firstDayOfWeek.getTime());
		
		//排班记录信息
		caller.setArrangeInfoList(detail.getArrangeInfoList());

		int week = DateUtils.getWeekInYear(firstDayOfWeek);
		String weekOfYear = "第" + week + "周";

		caller.getWeekTimeText().setText(day + StringUtils.EMPTY + weekOfYear);

		//第几周
		int which = detail.getWhich();

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(caller.getActivity());

		if (tokenKeeper.isLogin()) {
			
			//当前周第一天
			int year = DateUtils.getYear(firstDayOfWeek);
			List<Date> dayList = DateUtils.dateToWeek(year, which);
			long firstDayWeek = OtherUtils.maxLength(dayList.iterator().next().getTime());
			
			RequestParams params = new RequestParams();
			params.put("which", which);
			params.put("type", Constants.ARRANGE_ACTION_TYPE_ARRANGE);
			params.put("uid", tokenKeeper.getOpenId());
			params.put("circle_id", tokenKeeper.getUser().circleId());
			params.put("time", firstDayWeek);
			
			//取周视图数据
			ApiRequest.arrangeRecordDetailArrangeWeekView.request(caller, params);

		}

	}

}
